package com.quartzd.controller;

import com.quartzd.core.client.QuartzClient;
import com.quartzd.entity.JobInfo;
import com.quartzd.init.JobInit;
import com.quartzd.service.QuartzInfoService;
import com.quartzd.util.Func;
import org.springframework.web.bind.annotation.*;

/**
 * 描述：web api
 *
 * @author huxuehao
 **/

@RestController
@RequestMapping("/job")
public class JobController {
    private final QuartzClient quartzClient;
    private final QuartzInfoService quartzInfoService;

    public JobController(QuartzClient quartzClient, QuartzInfoService quartzInfoService) {
        this.quartzClient = quartzClient;
        this.quartzInfoService = quartzInfoService;
    }

    @PostMapping("/add")
    public Object add(@RequestBody JobInfo jobInfo) {
        jobInfo.setId(Func.uuid());
        return quartzInfoService.save(jobInfo);
    }

    @PostMapping("/update")
    public Object update(@RequestBody JobInfo jobInfo) {
        JobInfo job = quartzInfoService.getById(jobInfo.getId());
        if (job == null) {
            throw new RuntimeException("任务实例不存在");
        }
        if (job.getStatus() == 1) {
            throw new RuntimeException("任务实例运行中，请先关闭实例");
        }
        return quartzInfoService.updateById(jobInfo);
    }

    @GetMapping("/updateCron")
    public Object updateCron(@RequestParam String id, @RequestParam String cron) {
        JobInfo job = quartzInfoService.getById(id);
        if (job == null) {
            throw new RuntimeException("任务实例不存在");
        }
        if (job.getStatus() == 1) {
            throw new RuntimeException("任务实例运行中，请先关闭实例");
        }
        if (cron.equals(job.getCron())) {
            return true;
        }
        job.setCron(cron);
        return quartzInfoService.updateById(job);
    }

    @GetMapping("/delete")
    public Object delete(@RequestParam String id) throws ClassNotFoundException {
        JobInfo jobInfo = quartzInfoService.getById(id);
        if (jobInfo == null) {
            return true;
        }
        if (jobInfo.getStatus() == 1) {
            quartzClient.remove(JobInit.buildConfig(jobInfo));
        }

        return quartzInfoService.removeById(id);
    }

    @GetMapping("/start")
    public Object start(@RequestParam String id) throws ClassNotFoundException {
        JobInfo jobInfo = quartzInfoService.getById(id);
        if (jobInfo == null) {
            throw new RuntimeException("任务实例不存在");
        }
        if (jobInfo.getStatus() == 1) {
           return true;
        }
        quartzClient.create(JobInit.buildConfig(jobInfo));
        return true;
    }

    @GetMapping("/stop")
    public Object stop(@RequestParam String id) throws ClassNotFoundException {
        JobInfo jobInfo = quartzInfoService.getById(id);
        if (jobInfo == null) {
            throw new RuntimeException("任务实例不存在");
        }
        if (jobInfo.getStatus() == 0) {
            return true;
        }
        quartzClient.remove(JobInit.buildConfig(jobInfo));
        return true;
    }
}
